package com.arunsawad.ilertu.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.arunsawad.ilertu.R;
import com.arunsawad.ilertu.dto.Service;

public class ServicesAdapter extends BaseAdapter {

	private LayoutInflater layoutInflater;

	private List<Service> items = new ArrayList<Service>();

	public ServicesAdapter(Context context, List<Service> items) {
		layoutInflater = LayoutInflater.from(context);
		this.items = items;
	}

	public int getCount() {
		return items.size();
	}

	public Service getItem(int position) {
		return items.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ServiceViewHolder holder;
		Service item = items.get(position);
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.lv_services,
					null);
			holder = new ServiceViewHolder();
			holder.id = (TextView) convertView
					.findViewById(R.id.services_txtServiceId);
			holder.name = (TextView) convertView
					.findViewById(R.id.services_txtServiceName);
			holder.desc = (TextView) convertView
					.findViewById(R.id.services_txtServiceDesc);
			holder.image = (ImageView) convertView
					.findViewById(R.id.services_image);
			convertView.setTag(holder);
		} else {
			holder = (ServiceViewHolder) convertView.getTag();
		}
		holder.id.setText(String.valueOf(item.getId()));
		holder.name.setText(item.getName());
		holder.desc.setText(item.getDesc());
		if (item.getImage() != null) {
			holder.image.setImageBitmap(item.getImage());
		} else {
			// MY DEFAULT IMAGE
			holder.image.setImageResource(R.drawable.noimg_128);
		}
		return convertView;
	}

	static class ServiceViewHolder {
		TextView id;
		TextView name;
		TextView desc;
		ImageView image;
	}
}